package com.wu.gumomallproduct.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.wu.common.constant.ProductConstant;
import com.wu.gumomallproduct.product.dao.AttrAttrgroupRelationDao;
import com.wu.gumomallproduct.product.dao.AttrGroupDao;
import com.wu.common.entity.product.AttrAttrgroupRelationEntity;
import com.wu.common.entity.product.AttrGroupEntity;
import com.wu.common.entity.product.CategoryEntity;
import com.wu.gumomallproduct.product.service.CategoryService;
import com.wu.common.vo.product.AttrEntityRespVo;
import com.wu.common.vo.product.AttrEntityVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wu.common.utils.PageUtils;
import com.wu.common.utils.Query;

import com.wu.gumomallproduct.product.dao.AttrDao;
import com.wu.common.entity.product.AttrEntity;
import com.wu.gumomallproduct.product.service.AttrService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;


@Service("attrService")
public class AttrServiceImpl extends ServiceImpl<AttrDao, AttrEntity> implements AttrService {

    @Autowired
    private AttrAttrgroupRelationDao relationDao;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private AttrGroupDao attrGroupDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId, String attrType) {
        String key = (String) params.getOrDefault("key", "");
        LambdaQueryWrapper<AttrEntity> queryWrapper = new LambdaQueryWrapper<AttrEntity>();
        queryWrapper.eq(AttrEntity::getAttrType, "base".equalsIgnoreCase(attrType)?ProductConstant.AttrEnum.ATTR_TYPE_BASE.getCode():ProductConstant.AttrEnum.ATTR_TYPE_SALE.getCode());
        queryWrapper.and(StringUtils.isNotEmpty(key),w->w.eq(AttrEntity::getAttrId, key).or().like(AttrEntity::getAttrName,key));
        if (catelogId != 0L){
            queryWrapper.eq(AttrEntity::getCatelogId, catelogId);
        }

        IPage<AttrEntity> page = this.page(
                new Query<AttrEntity>().getPage(params),
                queryWrapper
        );
        PageUtils pageUtils = new PageUtils(page);
        pageUtils.setList(page.getRecords().stream().map(x -> {
            AttrEntityRespVo attrEntityRespVo = new AttrEntityRespVo();
            BeanUtils.copyProperties(x, attrEntityRespVo);
            AttrAttrgroupRelationEntity attrAttrgroupRelation = relationDao.selectOne(new LambdaQueryWrapper<AttrAttrgroupRelationEntity>().eq(AttrAttrgroupRelationEntity::getAttrId, x.getAttrId()));
            if (!ObjectUtils.isEmpty(attrAttrgroupRelation)) {
                AttrGroupEntity attrGroupEntity = attrGroupDao.selectById(attrAttrgroupRelation.getAttrGroupId());
                attrEntityRespVo.setGroupName(attrGroupEntity.getAttrGroupName());
            }
            CategoryEntity category = categoryService.getById(x.getCatelogId());
            attrEntityRespVo.setCatelogName(category.getName());
            return attrEntityRespVo;
        }).collect(Collectors.toList()));

        return pageUtils;
    }

    @Override
    @Transactional
    public void saveAttrEntityVo(AttrEntityVo attrEntityVo) {
        AttrEntity attrEntity = new AttrEntity();
        BeanUtils.copyProperties(attrEntityVo, attrEntity);
        this.save(attrEntity);
        if (attrEntity.getAttrType().compareTo(ProductConstant.AttrEnum.ATTR_TYPE_BASE.getCode()) == 0 && attrEntity.getAttrId()!= null){
            AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
            relationEntity.setAttrGroupId(attrEntityVo.getAttrGroupId());
            relationEntity.setAttrId(attrEntity.getAttrId());
            relationDao.insert(relationEntity);
        }
    }

    @Override
    public AttrEntityRespVo getAttrInfo(Long attrId) {
        AttrEntity attrEntity = this.getById(attrId);
        AttrEntityRespVo attrEntityRespVo = new AttrEntityRespVo();
        BeanUtils.copyProperties(attrEntity, attrEntityRespVo);
        if (attrEntity.getAttrType().compareTo(ProductConstant.AttrEnum.ATTR_TYPE_BASE.getCode()) == 0){
            AttrAttrgroupRelationEntity attrAttrgroupRelation = relationDao.selectOne(new LambdaQueryWrapper<AttrAttrgroupRelationEntity>().eq(AttrAttrgroupRelationEntity::getAttrId, attrEntity.getAttrId()));
            if (!ObjectUtils.isEmpty(attrAttrgroupRelation)) {
                attrEntityRespVo.setAttrGroupId(attrAttrgroupRelation.getAttrGroupId());
            }
        }
        Long[] catelogPath = categoryService.findCatelogPath(attrEntity.getCatelogId());
        attrEntityRespVo.setCatelogPath(catelogPath);
        return attrEntityRespVo;
    }

    @Override
    public void updateAttr(AttrEntityVo attr) {
        AttrEntity attrEntity = new AttrEntity();
        BeanUtils.copyProperties(attr, attrEntity);
        this.updateById(attrEntity);

        Wrapper<AttrAttrgroupRelationEntity> updateWrapper = new LambdaUpdateWrapper<AttrAttrgroupRelationEntity>()
                .eq(AttrAttrgroupRelationEntity::getAttrId, attrEntity.getAttrId());

        AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
        relationEntity.setAttrGroupId(attr.getAttrGroupId());
        relationEntity.setAttrId(attrEntity.getAttrId());
        Integer count = relationDao.selectCount(updateWrapper);
        int i = count > 0 ? relationDao.update(relationEntity, updateWrapper) :relationDao.insert(relationEntity);
    }

    @Override
    public List<AttrEntity> getAttrList(Long attrgroupId) {
        List<AttrAttrgroupRelationEntity> attrAttrgroupRelationEntities = relationDao.selectList(new LambdaQueryWrapper<AttrAttrgroupRelationEntity>().eq(AttrAttrgroupRelationEntity::getAttrGroupId, attrgroupId));
        List<Long> collect = attrAttrgroupRelationEntities.stream().map(x -> x.getAttrId()).collect(Collectors.toList());
        if (collect == null || collect.isEmpty()){
            return null;
        }
        List<AttrEntity> list = this.listByIds(collect);
        return list;
    }

    @Override
    public PageUtils getAttrNoList(Long attrgroupId, Map<String, Object> param) {
        String key = (String) param.getOrDefault("key", "");
        AttrGroupEntity attrGroupEntity = attrGroupDao.selectById(attrgroupId);
        List<AttrGroupEntity> attrGroupEntities = attrGroupDao.selectList(new LambdaQueryWrapper<AttrGroupEntity>().eq(AttrGroupEntity::getCatelogId, attrGroupEntity.getCatelogId()));
        List<Long> collect = attrGroupEntities.stream().map(x -> x.getAttrGroupId()).collect(Collectors.toList());
        List<AttrAttrgroupRelationEntity> relationEntities = relationDao.selectList(new LambdaQueryWrapper<AttrAttrgroupRelationEntity>().in(AttrAttrgroupRelationEntity::getAttrGroupId, collect));
        List<Long> list = relationEntities.stream().map(x -> x.getAttrId()).collect(Collectors.toList());
        IPage<AttrEntity> page = this.page(new Query<AttrEntity>().getPage(param), new LambdaQueryWrapper<AttrEntity>()
                .eq(AttrEntity::getCatelogId, attrGroupEntity.getCatelogId())
                .notIn(AttrEntity::getAttrId, list)
                .and(StringUtils.isNotEmpty(key),(w)-> w.eq(AttrEntity::getAttrId, key).or().like(AttrEntity::getAttrName,key)));
        PageUtils pageUtils = new PageUtils(page);
        return pageUtils;
    }


}